#!/usr/bin/env python
# -*- coding: utf-8 -*-

from core.poker import poker
from core.sort import sort
from collections import Counter
import random
'''
core.deck
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
一副扑克基类
'''
class deck:
    '''
    Attribute
        private __playowner__:
            Type:string
            Description:玩家
        private __player__:
            Type:Dict()
            Description:玩家手牌
        private __pokerlist__:
            Type:[]
            Description:一副(54张)扑克数组
        private __playlist__:
            Type:[]
            Description:出牌列表
    '''
    __playowner__=[]
    __player__=dict()
    __pokerlist__=[]
    __playlist__=[]

    def __init__(self):
        '''
        Feature
            __init__
        Decription
            初始化扑克
        '''
        for num in range(2,17):
            c_num=5
            if num>14:
                c_num=2
            for color in range(1,c_num):
                item=poker(num,color)
                self.__pokerlist__.append(item)

    def join(self,uid):
        '''
        Feature
            join
        Description
            玩家加入
        '''
        self.__playowner__.append(uid)
        self.__player__[uid]=[]

    def shuffle(self,s_num=2):
        '''
        Feature
            shuffle(self,s_num=2)
        Args
            s_num:
                default value:2
                descript:洗牌次数权重值 (洗牌次数=扑克张数*s_sum)
        '''
        a=0 #first offset
        b=0 #second offset
        for i in range(0,len(self.__pokerlist__)*s_num):
            a=random.randint(0,len(self.__pokerlist__)-1) #generate an random offset
            b=random.randint(0,len(self.__pokerlist__)-1) #generate an random offset
            if a==b:
                '''
                保证两个偏移量不能相同
                '''
                if i>0:
                    i=i-1 #如果相同者恢复此次洗牌次数扣减
            else:
                '''
                交换两张扑克位置(洗牌)
                '''
                temp=self.__pokerlist__[a]
                self.__pokerlist__[a]=self.__pokerlist__[b]
                self.__pokerlist__[b]=temp

    def deal(self,dealnum=1,pokernum=1):
        '''
        Feature
            deal(self,dealnum=1)
        Description
            发牌
        Args
            dealnum:
                default value:1
                description:每人手牌数量
            pokernum:
                default value:1
                description:每次发牌张数
        '''
        self.shuffle()
        playernum=len(self.__playowner__) #获取玩家数量
        if playernum:
            pokeroffset=len(self.__pokerlist__)-1 #获取扑克张数，确定偏移量
            deal_num = int(dealnum/pokernum)
            if dealnum%pokernum!=0:
                deal_num+=1
            for i in range(0,deal_num):
                if pokeroffset>=0: #保证扑克数量>0
                    '''
                    轮询为每一个玩家发一张扑克，从扑克数组最后一张发起；发完后不可再发
                    '''
                    if i*pokernum+pokernum>dealnum:
                        pokernum=dealnum-i*pokernum
                    for player in self.__player__:
                        p_num=1
                        while p_num!=0:
                            if pokeroffset>=0:
                                self.__player__[player].append(self.__pokerlist__[pokeroffset]) #发一张扑克给玩家
                                del self.__pokerlist__[pokeroffset] #将已发出的扑克从底牌中移除
                                pokeroffset=pokeroffset-1 #每次发一张扑克，偏移量向前移一位
                                p_num+=1
                                if p_num==pokernum+1:
                                    p_num=0


    def sort(self,orderlist,by='desc'):
        '''
        Feature
            sort(self,orderlist,by='desc')
        Description
            排序
        Args
            orderlist:[],需要排序的数组
            by:
                default value:desc
                description:desc[降序],asc[升序]
        '''
        if by.lower()=="desc":
            return sort().sort(orderlist)
        elif by.lower()=="asc":
            return sort().sortasc(orderlist)
